using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using TinyERP.Framework.Validation;
using Bamboo.Core.Common.Logger;
using TinyERP.Core.Business;
using TinyERP.Core.DTO;
namespace TinyERP.Core.View
{
    public class UserEditInfoForm : BasicEditInfoDialog
    {
		private UserDTO user = new UserDTO();
		private KLabel lblUserName;
		private TextBox txtUserName;
		private KLabel lblPassword;
		private TextBox txtPassword;
		private KLabel lblFullName;
		private TextBox txtFullName;
		private KLabel lblEmail;
		private TextBox txtEmail;
		private KLabel lblBirthday;
		private DateTextBox dtBirthday;
		private KLabel lblUserGroup;
		private ComboBox cbUserGroup;

		public UserDTO User
		{
			get 
			{
				return user;
			}
			set 
			{
				user = value;
				LoadEntityInfo();
			}
		}
		public IUserService UserService 
		{
			get
			{
				return (IUserService) ServiceLocator.GetService("UserServiceProxy");
			}
		}
		public IUserGroupService UserGroupService 
		{
			get 
			{
				return (IUserGroupService) ServiceLocator.GetService("UserGroupServiceProxy");
			}
		}	
		
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Name = "UserEditInfoForm";

			

			this.lblUserName = new KLabel();
			this.lblUserName.Location = new System.Drawing.Point(16, 16);
			this.lblUserName.Name = "lblUserName";
            this.lblUserName.Size = new System.Drawing.Size(100, 13);
            this.lblUserName.TabIndex = 0;
            this.lblUserName.Text = ModuleResourceManager.GetInstance().GetString("UserName", "TinyERP.Core");
            this.lblUserName.ResourceKey = "UserName";
            this.lblUserName.ResourceModuleKey = "TinyERP.Core";
            this.contentPanel.Controls.Add(lblUserName);
			this.txtUserName = new TextBox();			
			this.txtUserName.Location = new System.Drawing.Point(126, 16);
            this.txtUserName.Name = "txtUserName";
            this.txtUserName.Size = new System.Drawing.Size(266, 20);
            this.txtUserName.TabIndex = 1;
            this.contentPanel.Controls.Add(this.txtUserName);
            
			this.lblPassword = new KLabel();
			this.lblPassword.Location = new System.Drawing.Point(16, 41);
			this.lblPassword.Name = "lblPassword";
            this.lblPassword.Size = new System.Drawing.Size(100, 13);
            this.lblPassword.TabIndex = 0;
            this.lblPassword.Text = ModuleResourceManager.GetInstance().GetString("Password", "TinyERP.Core");
            this.lblPassword.ResourceKey = "Password";
            this.lblPassword.ResourceModuleKey = "TinyERP.Core";
            this.contentPanel.Controls.Add(lblPassword);
			this.txtPassword = new TextBox();			
			this.txtPassword.Location = new System.Drawing.Point(126, 41);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.Size = new System.Drawing.Size(266, 20);
            this.txtPassword.TabIndex = 2;
            this.contentPanel.Controls.Add(this.txtPassword);
            
			this.lblFullName = new KLabel();
			this.lblFullName.Location = new System.Drawing.Point(16, 66);
			this.lblFullName.Name = "lblFullName";
            this.lblFullName.Size = new System.Drawing.Size(100, 13);
            this.lblFullName.TabIndex = 0;
            this.lblFullName.Text = ModuleResourceManager.GetInstance().GetString("FullName", "TinyERP.Core");
            this.lblFullName.ResourceKey = "FullName";
            this.lblFullName.ResourceModuleKey = "TinyERP.Core";
            this.contentPanel.Controls.Add(lblFullName);
			this.txtFullName = new TextBox();			
			this.txtFullName.Location = new System.Drawing.Point(126, 66);
            this.txtFullName.Name = "txtFullName";
            this.txtFullName.Size = new System.Drawing.Size(266, 20);
            this.txtFullName.TabIndex = 3;
            this.contentPanel.Controls.Add(this.txtFullName);
            
			this.lblEmail = new KLabel();
			this.lblEmail.Location = new System.Drawing.Point(16, 91);
			this.lblEmail.Name = "lblEmail";
            this.lblEmail.Size = new System.Drawing.Size(100, 13);
            this.lblEmail.TabIndex = 0;
            this.lblEmail.Text = ModuleResourceManager.GetInstance().GetString("Email", "TinyERP.Core");
            this.lblEmail.ResourceKey = "Email";
            this.lblEmail.ResourceModuleKey = "TinyERP.Core";
            this.contentPanel.Controls.Add(lblEmail);
			this.txtEmail = new TextBox();			
			this.txtEmail.Location = new System.Drawing.Point(126, 91);
            this.txtEmail.Name = "txtEmail";
            this.txtEmail.Size = new System.Drawing.Size(266, 20);
            this.txtEmail.TabIndex = 4;
            this.contentPanel.Controls.Add(this.txtEmail);
            
			this.lblBirthday = new KLabel();
			this.lblBirthday.Location = new System.Drawing.Point(16, 116);
			this.lblBirthday.Name = "lblBirthday";
            this.lblBirthday.Size = new System.Drawing.Size(100, 13);
            this.lblBirthday.TabIndex = 0;
            this.lblBirthday.Text = ModuleResourceManager.GetInstance().GetString("Birthday", "TinyERP.Core");
            this.lblBirthday.ResourceKey = "Birthday";
            this.lblBirthday.ResourceModuleKey = "TinyERP.Core";
            this.contentPanel.Controls.Add(lblBirthday);
			this.dtBirthday = new DateTextBox();			
			this.dtBirthday.Location = new System.Drawing.Point(126, 116);
            this.dtBirthday.Name = "dtBirthday";
            this.dtBirthday.Size = new System.Drawing.Size(266, 20);
            this.dtBirthday.TabIndex = 5;
            this.contentPanel.Controls.Add(this.dtBirthday);
            

			this.lblUserGroup = new KLabel();
			this.lblUserGroup.Location = new System.Drawing.Point(16, 141);
			this.lblUserGroup.Name = "lblUserGroup";
            this.lblUserGroup.Size = new System.Drawing.Size(100, 13);
            this.lblUserGroup.TabIndex = 0;
            this.lblUserGroup.ResourceKey = "UserGroup";
            this.lblUserGroup.ResourceModuleKey = "TinyERP.Core";

            this.lblUserGroup.Text = ModuleResourceManager.GetInstance().GetString("UserGroup", "TinyERP.Core");
            this.contentPanel.Controls.Add(lblUserGroup);
			

			this.cbUserGroup = new ComboBox();
			this.cbUserGroup.Location = new System.Drawing.Point(126, 141);
            this.cbUserGroup.Name = "cbUserGroup";
            this.cbUserGroup.Size = new System.Drawing.Size(266, 20);
            this.cbUserGroup.TabIndex = 6;
			this.contentPanel.Controls.Add(cbUserGroup);
						this.deleteButton.TabIndex = 8;
			this.saveButton.TabIndex = 9;
			this.closeButton.TabIndex = 10;
			

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

			this.Size = new System.Drawing.Size(502, 405);
            this.contentPanel.ResumeLayout(false);
            this.contentPanel.PerformLayout();
            this.ResumeLayout(false);
        }

        #endregion


		public UserEditInfoForm() : base()
		{
			InitializeComponent();
			LoadEntityInfo();
		}
		
		protected override void AddValidationRules() 
		{
			//TODO: use validator to add validation rules on your input controls
			//Example: validator.AddValidationRule(new RequiredFieldRule(txtUserName, "User name"));
		}
		
		
		
		private void PrepareData() 
		{
			LoadDataOfUserGroup();
		}
		
		private void LoadDataOfUserGroup() 
		{
			long totalRecord = 0;
            IList userGroupList = UserGroupService.GetAllUserGroup("ID", Bamboo.Core.Data.SortDirection.ASCENDING, out totalRecord);
            
            cbUserGroup.ValueMember = "ID";
            cbUserGroup.DisplayMember = "Name";
            cbUserGroup.DataSource = userGroupList; 
		}
		protected override void LoadEntityInfo() 
		{
			PrepareData();
		    if(user==null) 
		    {
				user = new UserDTO();
		    }
			txtUserName.Text = user.UserName;
			txtPassword.Text = user.Password;
			txtFullName.Text = user.FullName;
			txtEmail.Text = user.Email;
			dtBirthday.Value = user.Birthday;
			cbUserGroup.SelectedValue = user.UserGroupID;
			deleteButton.Visible = (user.ID != 0) ;
		}
		
		protected override bool ValidateInputData() 
		{
			if(!base.Validate()) 
			{
				return false;
			}
			//TODO: add your business validation here
			return true;
		}
		
		private bool UpdateEntityInfoFromInput() 
		{
			if(!ValidateInputData()) 
			{
				return false;
			}
			user.UserName = txtUserName.Text;
			user.Password = txtPassword.Text;
			user.FullName = txtFullName.Text;
			user.Email = txtEmail.Text;
			user.Birthday = dtBirthday.Value;
			if(cbUserGroup.SelectedValue != null) 
			{
				user.UserGroupID = (long) cbUserGroup.SelectedValue;
			}
			return true;
		}
        
        public override void OnSave()
        {
			if(!UpdateEntityInfoFromInput()) 
			{
				return;
			}
			try {
				if(user.ID==0) 
				{
					UserService.SaveUser(user);
				}
				else 
				{
					UserService.UpdateUser(user);
				}
				MessageBox.Show(CommonMessage.GetSaveSuccessMessage(GetEntityResourceKey()));
                InvokeOnNeedReloadData();
                InvokeCloseDialog();
			} 
			catch(Exception ex) 
			{
				LogManager.BusinessLog.Debug(ex);			
				MessageBox.Show(CommonMessage.GetCanNotSaveError(GetEntityResourceKey()));
			}
        }

        public override void OnDelete()
        {
			try 
			{
                if (ConfirmDelete() == DialogResult.OK)
                {
					UserService.DeleteUserByID(user.ID);			
					MessageBox.Show(CommonMessage.GetDeleteSuccessMessage(GetEntityResourceKey()));
                    InvokeOnNeedReloadData();
                    InvokeCloseDialog();
				}
			}
			catch(Exception ex) 
			{
                LogManager.BusinessLog.Debug(ex);			
				MessageBox.Show(CommonMessage.GetCanNotDeleteError(GetEntityResourceKey()));
			}
        }

        
        public override string GetHeaderText()
        {
            return ModuleResourceManager.GetInstance().GetString("UserEditForm", "TinyERP.Core");
        }

        private string GetEntityResourceKey()
        {
            return ModuleResourceManager.GetInstance().GetString("User", "TinyERP.Core");
        }

        private DialogResult ConfirmDelete()
        {
            return MessageBox.Show(CommonMessage.GetDeleteConfirmMessage(GetEntityResourceKey()),
                CommonMessage.GetConfirmationText(), MessageBoxButtons.OKCancel);
        }

	}
}

